<template>
  <div class="app-container">
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        label="分区序号"
        align="center"
        width="120"
      />
      <el-table-column
        prop="startAngle"
        label="开始角度"
        align="center"
      >
        <template #default="scope">
          <el-input-number v-model="scope.row.startAngle" controls-position="right" :min="0" :max="360" placeholder="开始角度" />
        </template>
      </el-table-column>
      <el-table-column
        prop="endAngle"
        label="结束角度"
        align="center"
      >
        <template #default="scope">
          <el-input-number v-model="scope.row.endAngle" controls-position="right" :min="0" :max="360" placeholder="结束角度" />
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        align="center"
        width="240"
      >
        <template slot="header">
          <el-link type="primary" icon="el-icon-plus" size="mini" @click="addRow">新增</el-link>
        </template>
        <template slot-scope="scope">
          <el-link type="danger" icon="el-icon-delete" size="mini" @click="deleteRow(scope.$index)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="h12" />
    <el-button type="primary" icon="el-icon-document" @click="submit">提交</el-button>
  </div>
</template>

<script>
import { getOne, addOne } from '@/api/wsgArea'
export default {
  props: {
    serialno: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      tableData: []
    }
  },
  created() {
    this.refresh()
  },
  methods: {
    refresh() {
      this.getOne(this.serialno)
    },
    async getOne(serialno) {
      const { data } = await getOne(serialno)
      this.tableData = data.content || []
    },
    async submit() {
      const reqData = {
        serialno: this.serialno,
        content: this.tableData
      }
      const { msg } = await addOne(this.serialno, reqData)
      this.$message.success(msg)
    },
    addRow() {
      this.tableData.push({
        startAngle: '',
        endAngle: ''
      })
    },
    deleteRow(index) {
      this.tableData.splice(index, 1)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
